import 'package:flutter/material.dart';
import 'package:shining_red_star/widgets/focus_manage/focus_widget.dart';

import '../definitions/color.dart';
import '../definitions/icon_font.dart';

class AppIcon extends StatefulWidget {
  final FocusNode focusNode;
  final bool? autofocus;
  final void Function()? onConfirm;
  final String name;
  final int? backgroundColor;
  final IconFontValue icon;

  AppIcon({
    super.key,
    FocusNode? focusNode,
    this.autofocus,
    this.onConfirm,
    this.backgroundColor,
    required this.name,
    required this.icon,
  }) : focusNode = focusNode ?? FocusNode();

  @override
  State<AppIcon> createState() => _AppIconState();
}

class _AppIconState extends State<AppIcon> with TickerProviderStateMixin {
  @override
  Widget build(BuildContext context) {
    const double size = 64;

    bool showFocusStatus = widget.focusNode.hasFocus;
    return FocusWidget(
      focusNode: widget.focusNode,
      autofocus: widget.autofocus,
      onConfirm: widget.onConfirm,
      onFocusChange: (bool value) {
        setState(() {});
      },
      child: Column(
        children: [
          Container(
            width: size,
            height: size,
            margin: const EdgeInsets.only(bottom: 8),
            decoration: BoxDecoration(
              color: widget.backgroundColor != null
                  ? Color(widget.backgroundColor!)
                  : Colors.white,
              borderRadius: BorderRadius.circular(15),
            ),
            child: Icon(
              widget.icon.value,
              size: size / 1.2,
            ),
          ),
          Text(
            widget.name,
            overflow: TextOverflow.ellipsis,
            style: TextStyle(
                fontSize: 14,
                color: showFocusStatus ? SchemeColor.seedColor.value : null,
                fontWeight: showFocusStatus ? FontWeight.bold : null),
          ),
        ],
      ),
    );
  }
}
